import tkinter as tk
import random
import time

import bfs
import Astar
import tkinter.messagebox


win=tk.Tk()
win.geometry("720x480")
win.title("graph algorithm")
PlayArea=tk.Canvas(win,width=720,height=480,bg="white")
maze_size=tk.IntVar()


class GUI:
    def __init__(self):
        self.win_width=480
        self.win_height=480
        self.w_step=0
        self.h_step=0#一格代表的像素

    def generate_graph(self):
        size=maze_size.get()
        if size:
            self.w_step=self.win_width/size
            self.h_step=self.win_height/size
            bfs.maze_map=[[0 for i in range(size)]for j in range(size)]
            #画地图
            for y in range(size):
                for x in range(size):
                    PlayArea.create_rectangle(x*self.w_step,y*self.h_step,(x+1)*self.w_step,(y+1)*self.h_step,fill="gray",outline="black")
                    PlayArea.pack()
            #画起点和终点
            PlayArea.create_rectangle(0,0,self.w_step,self.h_step,fill="blue",outline="black")
            PlayArea.pack()
            PlayArea.create_rectangle((size-1)*self.w_step,(size-1)*self.h_step,size*self.w_step,size*self.h_step,fill="red",outline="black")
            PlayArea.pack()
            #画随机障碍
            for index in range((int)((size**2)*0.3)):
                i=random.randint(0,size-1)
                j=random.randint(0,size-1)
                if i+j>0 and i*j !=(size-1)**2:
                    bfs.maze_map[j][i]=1
                    PlayArea.create_rectangle(i*self.w_step,j*self.h_step,(i+1)*self.w_step,(j+1)*self.h_step,fill="black",outline="black")
                    PlayArea.pack()
            # print(bfs.maze_map)
        else:
            tk.messagebox.showinfo(title="提示",message="请先选择迷宫大小")
            return

    def bfs_solve(self):
        size=maze_size.get()
        if bfs.algo_bfs(bfs.maze_map,PlayArea,bfs.pos(0,0),bfs.pos(size-1,size-1),self.w_step,self.h_step):
             tk.messagebox.showinfo(title="提示",message="路径查找成功")
        else:
            tk.messagebox.showwarning(title="提示",message="路径查找失败")
    def astar_solve(self):
        size=maze_size.get()
        if Astar.a_star(bfs.maze_map,PlayArea,bfs.pos(0,0),bfs.pos(size-1,size-1),self.w_step,self.h_step):
            tk.messagebox.showinfo(title="提示",message="路径查找成功")
        else:
            tk.messagebox.showwarning(title="提示",message="路径查找失败")




gui=GUI()

create_maze_bt=tk.Button(win,text="生成迷宫",command=gui.generate_graph,width=10,height=1)
create_maze_bt.place(x=600,y=150)
maze_size_low=tk.Radiobutton(win,text="10",variable=maze_size,value=10)
maze_size_low.place(x=600,y=180)
maze_size_middle=tk.Radiobutton(win,text="20",variable=maze_size,value=20)
maze_size_middle.place(x=600,y=210)
maze_size_high=tk.Radiobutton(win,text="40",variable=maze_size,value=40)
maze_size_high.place(x=600,y=240)
bfs_bt=tk.Button(win,text="bfs",command=gui.bfs_solve,width=10,height=1)
bfs_bt.place(x=600,y=280)
a_star_bt=tk.Button(win,text="A*",command=gui.astar_solve,width=10,height=1)
a_star_bt.place(x=600,y=320)
win.mainloop()